package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.HousingActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.HousingModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 * 我的出租房
 */

public class HousingActivityAdapter extends BaseAdapter {

    private Context mContext;
    List<HousingModel> model= new ArrayList<>();
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    public HousingActivityAdapter(Context context) {
        this.mContext = context;
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem( List<HousingModel> model){
        this.model=model;
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_hous_listvew, null);
            holder.item_image = (ImageView) convertView
                    .findViewById(R.id.item_image);
            holder.item_title = (TextView) convertView
                    .findViewById(R.id.item_title);
            holder.item_site = (TextView) convertView
                    .findViewById(R.id.item_site);
            holder.time = (TextView) convertView
                    .findViewById(R.id.time);
            holder.tingshi = (TextView) convertView
                    .findViewById(R.id.tingshi);
            holder.item_moneys = (TextView) convertView
                    .findViewById(R.id.item_moneys);
            holder.rejected_deleting_layout = (LinearLayout) convertView
                    .findViewById(R.id.rejected_deleting_layout);

            holder.modification = (TextView) convertView
                    .findViewById(R.id.modification);
            holder.rejected = (TextView) convertView
                    .findViewById(R.id.rejected);
            holder.deleting = (TextView) convertView
                    .findViewById(R.id.deleting);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.item_image.setImageResource(R.mipmap.imhouse);
        final HousingModel bModel = model.get(position);
        String[] split = bModel.getPic().split("\\|");
        imageLoader.displayImage(Constant.HTTP + split[0],   holder.item_image, options);
        holder.item_title.setText(bModel.getTitle());

        holder.item_site.setText(bModel.getCommunity());
        holder.item_moneys.setText(bModel.getMnthly()+"元/月");


        if (bModel.getAudit().equals("2")){
            holder.time.setText(Html.fromHtml("审核中"));
            holder.rejected_deleting_layout.setVisibility(View.GONE);
        }else  if (bModel.getAudit().equals("3")){
            holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
            holder.rejected.setVisibility(View.INVISIBLE);
            holder.time.setText(Html.fromHtml("发布：<font color='#333333'>"+bModel.getRejecttime()+"</font>"));
        }else  if (bModel.getAudit().equals("4")){

            holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
            holder.rejected.setVisibility(View.VISIBLE);
            holder.time.setText(Html.fromHtml("<font color='#FD1142'>驳回："+bModel.getRejecttime()+"</font>"));
        }else  if (bModel.getAudit().equals("5")){

            holder.rejected_deleting_layout.setVisibility(View.VISIBLE);
            holder.rejected.setVisibility(View.VISIBLE);
            holder.time.setText(Html.fromHtml("<font color='#FD1142'>回收："+bModel.getRejecttime()+"</font>"));
        }
        holder.tingshi.setText(bModel.getRoom()+"    "+bModel.getMode());
        holder.modification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HousingActivity)mContext).processModification(bModel.getId());
            }
        });
        holder.deleting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HousingActivity)mContext).processdetails(bModel.getId());
            }
        });
        holder.rejected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((HousingActivity)mContext).processreject();
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView item_image;
        TextView item_title;
        TextView rejected;
        TextView item_site;
        TextView time;
        TextView tingshi;
        TextView item_moneys;
        TextView modification;
        TextView deleting;

        LinearLayout rejected_deleting_layout;
    }
}


